<?php
require_once('./includes/headers.inc.php');
require_once('./top.php');

$error = FALSE;
$completed = FALSE;
$notcompleted = FALSE;
$blockcompleted = FALSE;
$comment = '';
$blocks = 1;

if($_POST) {
    $checkinId = $checkinMgr->add($_SESSION['user']['id'], $_POST);
    if($checkinId) {
        unset($_SESSION['checkin']['route_id']);
        header('Location: /complete.php?id=' . $checkinId);
        exit();
    } else {
        $error = $msgMgr->get('checkin-error');
        $completed = (isset($_POST['completed']) && $_POST['completed'] == 0 ? 'checked' : '');
        $notcompleted = (isset($_POST['completed']) && $_POST['completed'] == -1 ? 'checked' : '');
        $blockcompleted = (isset($_POST['completed']) && $_POST['completed'] > 1 ? 'checked' : '');
        $comment = $_POST['comment'];
        $blocks = (int)$_POST['blocks'];
    }
}

//route id should be in GET or session
if(isset($_GET['route_id'])) {
    $route_id = $_GET['route_id'];
    $_SESSION['checkin']['route_id'] = $route_id;
} elseif(isset($_SESSION['checkin']['route_id'])) {
    $route_id = $_SESSION['checkin']['route_id'];
} else {
    $_SESSION['msg'] = $msgMgr->get('checkin-invalid-error');
    header('Location: /');
    exit();
}

//location id should be in session
if(!isset($_SESSION['checkin']['location_id'])) {
    //take from route if not
    $location_id = $routeMgr->getRouteLocationId($route_id);
} elseif(isset($_SESSION['checkin']['location_id'])) {
    $location_id = $_SESSION['checkin']['location_id'];
} else {
    $_SESSION['msg'] = $msgMgr->get('checkin-invalid-error');
    header('Location: /');
    exit();
}

$location = $locationMgr->get($location_id);
$route = $routeMgr->get($route_id);

$location_info = '<a href="/viewlocation.php?id=' . $location['id'] . '">' . $location['name'] . '</a>';
$route_info = '<span class="colour ' . $route['colour'] . '"><a href="/viewroute.php?id=' . $route['id'] . '">' . $route['colour'] . ' ' . $route['difficulty'] . ($route['is_short'] ? ' short' : '') . '</a></span>' . ($route['name'] ? ' <small><i>' . $route['name'] . '</i></small>' : '');

$replaces = array(
    'route_id'          => $route['id'],
    'route'             => $route_info,
    'location'          => $location_info,
    'completed'         => $completed,
    'notcompleted'      => $notcompleted,
    'blockcompleted'    => $blockcompleted,
    'comment'           => $comment,
    'blocks'            => $blocks,
    'error'             => $msgMgr->formatMsg($error),
);
$lang = array(
    'header_checkin'                => $langMgr->get('header_checkin'),
    'text_checkin_sub'              => $langMgr->get('text_checkin_sub'),
    'text_checkin_completed'        => $langMgr->get('text_checkin_completed'),
    'text_checkin_blockcompleted'   => $langMgr->get('text_checkin_blockcompleted'),
    'text_checkin_blocksuffix'      => $langMgr->get('text_checkin_blocksuffix'),
    'text_checkin_notcompleted'     => $langMgr->get('text_checkin_notcompleted'),
    'text_checkin_comment'          => $langMgr->get('text_checkin_comment'),
    'btn_checkin'                   => $langMgr->get('btn_checkin'),
);
$templateMgr->loadContent(basename(__FILE__, '.php'), array_merge($replaces, $lang));
?>
